package com.cyl.wms.controller;

import com.cyl.wms.domain.Address;
import com.cyl.wms.pojo.query.AddrQuery;
import com.cyl.wms.service.AddrService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 主页用到的Controller
 *
 * @author zcc
 * @date 2022-08-05
 */
@Api("主页接口列表")
@RestController
@RequestMapping("/wms/main")
public class MainController extends BaseController {
    @Autowired
    private AddrService service;

    @ApiOperation("查询检测仪与直管段库存")
    @PostMapping("/inventoryData")
    public ResponseEntity<Map> inventoryData() {
        Map data = new HashMap();
        Random random = new Random();
        data.put("countDevices",random.nextInt(100));
        data.put("countTubes",random.nextInt(100));
        Map res = new HashMap();
        res.put("data",data);
        return ResponseEntity.ok(res);
    }

    @ApiOperation("导出地址码列表")
    @PreAuthorize("@ss.hasPermi('wms:addr:export')")
    @Log(title = "地址码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(AddrQuery query) {
        List<Address> list = service.selectList(query, null);
        ExcelUtil<Address> util = new ExcelUtil<>(Address.class);
        return ResponseEntity.ok(util.writeExcel(list, "地址码数据"));
    }

    @ApiOperation("获取地址码详细信息")
    @PreAuthorize("@ss.hasPermi('wms:addr:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<Address> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增地址码")
    @PreAuthorize("@ss.hasPermi('wms:addr:add')")
    @Log(title = "地址码", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Address address) {
        return ResponseEntity.ok(service.insert(address));
    }

    @ApiOperation("修改地址码")
    @PreAuthorize("@ss.hasPermi('wms:addr:edit')")
    @Log(title = "地址码", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody Address address) {
        return ResponseEntity.ok(service.update(address));
    }

    @ApiOperation("删除地址码")
    @PreAuthorize("@ss.hasPermi('wms:addr:remove')")
    @Log(title = "地址码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
